<?php

namespace app\admin\controller;

use app\admin\model\AdminAdmin;
use app\common\service\UploadService;
use support\Request;
use support\Response;
use think\facade\Db;

class Index extends Base
{
    public function index(Request $request)
    {
        return view('index/index', [
            //'nickname'  => get_field('admin_admin',Session('admin'),'nickname')
            'nickname' => session('admin')['nickname'],
        ]);
    }

    public function view(Request $request)
    {
        return view('index/view', ['name' => 'webman']);
    }

    public function file(Request $request)
    {
        $file = $request->file('upload');
        if ($file && $file->isValid()) {
            $file->move(public_path().'/files/myfile.'.$file->getUploadExtension());
            return json(['code' => 0, 'msg' => 'upload success']);
        }
        return json(['code' => 1, 'msg' => 'file not found']);
    }

    //欢迎页
    public function home(Request $request)
    {
        return view('index/home', [
            'os'      => PHP_OS,
            'space'   => round((disk_free_space('.') / (1024 * 1024)), 2).'M',
            'addr'    => $request->getRemoteIp(),
            'run'     => $request->protocolVersion(),
            'php'     => PHP_VERSION,
            'php_run' => php_sapi_name(),
            'mysql'   => function_exists('mysql_get_server_info') ? mysql_get_server_info() : Db::query('SELECT VERSION() as mysql_version')[0]['mysql_version'],
            'webman'  => '1.0.0',
            'upload'  => ini_get('upload_max_filesize'),
            'max'     => ini_get('max_execution_time').'秒',
        ]);
    }

    //菜单
    public function menu(Request $request)
    {
        $debug = env('APP_DEBUG');
        $menu = get_tree(Session('admin')['menu']);
        if ($debug == true && Session('admin')['id'] == 1) {
            $menu[] = [
                "id"    => -1,
                "pid"   => 0,
                "title" => "CRUD生成",
                "icon"  => "layui-icon layui-icon-util",
                "href"  => APP_ADMIN."/crud/index",
                "type"  => 1,
            ];
        }
        return json($menu);
    }

    //修改密码
    public function pass(Request $request)
    {
        if ($request->post()) {
            if (!empty(session('admin')['id'])) {
//                (new \app\admin\model\AdminAdmin)->where('id',session('admin')['id'])->update(['password' => set_password(trim($request->post('password')))]);
            }
            (new AdminAdmin)->logout();
            return json(['msg' => '修改成功', 'code' => 200, 'data' => '/login/index']);
        }
        return view('index/pass');
    }

    //清除缓存
    public function cache(Request $request)
    {
        return json(['code' => 200, 'msg' => '清理成功']);
    }

    /**
     * 通用上传
     * @param  Request  $request
     * @return Response
     */
    public function upload(Request $request)
    {
        return json(['code' => 201, 'msg' => '演示禁止上传']);
        $file = $request->file('file');
        if ($file && $file->isValid()) {
            $res = UploadService::commonFile($file);
        } else {
            return json(['code' => 0, 'msg' => 'file not found']);
        }
        return json(['code' => $res['code'], 'msg' => $res['msg'], 'data' => $res['data']]);
    }

    public function test()
    {
        return json(['data' => 'test']);
    }
}
